#include<stdio.h>
#include<unistd.h>
#include<pthread.h>

/*
This is child thhread
cleanup2,arg=efgh
cleanup1,arg=abcd

 *
 */

void cleanup1(void * arg){
	printf("cleanup1,arg=%s\n", (char *)arg);
}

void cleanup2(void * arg){
	printf("cleanup2,arg=%s\n", (char *)arg);
}

void * func(void *arg){
	printf("This is child thhread\n");
	//设置取消点的类型是 立即取消。不然的话只能等待取消点才能取消
	pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
	pthread_cleanup_push(cleanup1, "abcd");
	pthread_cleanup_push(cleanup2, "efgh");
	{
		sleep(1);
	}
	pthread_cancel(pthread_self());
	printf("should not print\n");
	while(1){
		printf("sleep\n");
		sleep(1);
	}
	pthread_exit("thread return");
	pthread_cleanup_pop(1);
	pthread_cleanup_pop(1);
	sleep(10);

}

int main(){
	pthread_t tid;
	void * retv;
	
	pthread_create(&tid, NULL, func, NULL);
	sleep(1);
	pthread_join(tid, &retv);
	//printf("thread ret= %s\n", (char *)retv);
}
